<?php
include_once("dbconnect/db.php");
include_once("class/Common.php");
Class ResultList
{
	var $errorMessage;
	
	var $categoryId;
	
	var $resultList = "";
	
	var $nowPageNum = 1;
	
	var $topLink="";
	
	var $topTitle="";
	
	var $totalLineNum = 0;
	
	var $fistLineNum = 0;
	
	var $lastLineNum = 0;
	
	var $orderType = "1";
	
	var $pageLinkHtml = "";
	
	// 新着順リンク
	var $orderByDateLink = "";
	
	// 人気順リンク
	var $orderByClickLink = "";
	
	var $keyword = "";
	
	var $tagId = "";
	
	var $actionHtml = "";
	
	var $categoryFlag = "";
	
	// 毎Eージのレコード数
	var $RECORD_NUMBER_PER_PAGE = 5;
	
	function mytask()
	{
		$this->init();
		$this->getActionHtml();
		$this->getOrderByLink();
		$this->getTopLinkAndTitle();
		$this->getResultList($this->nowPageNum);
		
		// キーワードが存在の場合
		if($this->keyword != "" && $this->keyword != null)
		{
			// 履歴を更新する
			$this->insertOrUpdate_search_history($this->keyword);
		}
	}
	
	function init()
	{
		if(isset($_GET["nowPage"]))
		{
			$this->nowPageNum = $_GET["nowPage"];			
		}
		if(isset($_GET["orderType"]))
		{
			$this->orderType = $_GET["orderType"];			
		}
		if(isset($_GET["categoryId"]))
		{
			$this->categoryId = $_GET["categoryId"];			
		}
		if(isset($_REQUEST["keyword"]))
		{
			$this->keyword = mb_convert_encoding($_REQUEST["keyword"],"UTF-8", "SJIS");
			
			if(isset($_GET["keyword"]))
			{
				$this->keyword = mb_convert_encoding(urldecode($_GET["keyword"]),"UTF-8", "SJIS");
			}
		}
		if(isset($_GET["tagId"]))
		{
			$this->tagId = $_GET["tagId"];
		}
		if(isset($_GET["categoryFlag"]))
		{
			$this->categoryFlag = $_GET["categoryFlag"];
		}
	}
	
	function getOrderByLink()
	{
		// 新着順
		$this->orderByDateLink = "result_list.php?categoryId=".$this->categoryId.
								"&nowPage=".$this->nowPageNum."&orderType=1".
								"&keyword=".$this->keyword."&tagId=".$this->tagId;
		
		// 人気順
		$this->orderByClickLink = "result_list.php?categoryId=".$this->categoryId.
								"&nowPage=".$this->nowPageNum."&orderType=2".
								"&keyword=".$this->keyword."&tagId=".$this->tagId;
	}

	
	function getTopLinkAndTitle()
	{
		if($this->categoryId != "" && $this->tagId == "")
		{
			$sql = "select `category_name` from `category_mst` where `category_id` =";
			$sql = $sql.$this->categoryId;
			$result = mysql_query($sql);		
			$data = mysql_fetch_row($result);		
			
			$this->topLink = "<a href='top_page.php'>トップ</a>>".$data[0];

			$this->topTitle = "「".$data[0]."」カテゴリ動画一覧";
		}
		else if($this->tagId != "")
		{
			$sql = "SELECT cat2.category_id, cat2.category_name, cat1.category_id, cat1.category_name 
			FROM   category_mst cat1 join  category_mst cat2 on cat1.pid=cat2.category_id 
			WHERE  cat1.category_id=$this->categoryId and cat1.del_flag=0";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);
			$this->topLink = "<a href=/top_page.php>トップ</a>>".'<a name="category" href="categorylist.php">'.$row[1].'</a>>'
			.'<a name="subcategory" href="subcategorylist.php?bigCatId='.$row[0].'">'.$row[3].'</a>>';

			$sql = "SELECT tag_id,tag_name 
			FROM   tag_mst 
			WHERE  category_id=$this->categoryId
			AND    tag_id = $this->tagId
			ORDER BY entry_cnt desc
			limit 0,15";
			$result = mysql_query($sql);
			while($row=mysql_fetch_array($result)) 
			{
				$this->topLink = $this->topLink.'<a href="taglist.php?subCatId='.$this->categoryId.'">'.$row[1].'</a>>';
				
				$this->topTitle = "「".$row[1]."」カテゴリ動画一覧";
			}			

		}
		else if($this->categoryFlag == "1")
		{
			$this->topLink = "<a href=/top_page.php>トップ</a>>"."<a href=categorylist.php>カテゴリ一覧</a>>".$this->keyword;
			
			$this->topTitle = "「".$this->keyword."」動画一覧";
		}
		else
		{
			$this->topLink = "<a href=top_page.php>トップ</a>>".$this->keyword;
			
			if($this->keyword != "" && $this->keyword != null)
			{
				$this->topTitle = "「".$this->keyword."」動画一覧";				
			}
			else
			{
				$this->topTitle = "動画一覧";			
			}

		}
	}
	
	function getActionHtml()
	{
		$this->actionHtml = "result_list.php";
	}
	
	function getSelectSql()
	{
		$sql = "";
		// タグ検索の場合
		if($this->tagId != "")
		{
			$sql = "select `entrylist`.`title`,`videolist`.`cid`,`videolist`.`video_id`,`videolist`.`time`,`videolist`.`up_date`,`videolist`.`view_cnt_y`,`videolist`.`rating_avg` ";
			$sql = $sql."from `entrylist`,`videolist`,`tag_list` ";
			$sql = $sql."where `tag_list`.`category_id` =  ";
			$sql = $sql.$this->categoryId." ";
			$sql = $sql."and `tag_list`.`tag_id`= ";
			$sql = $sql.$this->tagId." ";
			$sql = $sql."and `videolist`.`site_id` = 1 ";
			$sql = $sql."and `videolist`.`open_flag` = 1 ";
			$sql = $sql."and `entrylist`.`open_flag` = 1 ";
			$sql = $sql."and `videolist`.`cid` is not null ";
			$sql = $sql."and `tag_list`.`entry_id` = `entrylist`.`entry_id` ";
			$sql = $sql."and `entrylist`.`entry_id` = `videolist`.`entry_id` ";	
			/*
			if($this->keyword != "")
			{
				$sql = $sql."and `entrylist`.`title` like binary '%$this->keyword%'";
			}*/		
		}
		// カテゴリ検索の場合
		else if ($this->categoryId != "")
		{
			$sql = "select `entrylist`.`title`,`videolist`.`cid`,`videolist`.`video_id`,`videolist`.`time`,`videolist`.`up_date`,`videolist`.`view_cnt_y`,`videolist`.`rating_avg` ";
			$sql = $sql."from `entrylist`,`videolist`,`urllist` ";
			$sql = $sql."where `urllist`.`category_id` =  ";
			$sql = $sql.$this->categoryId." ";
			$sql = $sql."and `videolist`.`site_id` = 1 ";
			$sql = $sql."and `videolist`.`open_flag` = 1 ";
			$sql = $sql."and `urllist`.`open_flag` = 1 ";
			$sql = $sql."and `entrylist`.`open_flag` = 1 ";
			$sql = $sql."and `videolist`.`cid` is not null ";
			$sql = $sql."and `urllist`.`urllist_id` = `entrylist`.`urllist_id` ";
			$sql = $sql."and `entrylist`.`entry_id` = `videolist`.`entry_id` ";
			/*
			if($this->keyword != "")
			{
				$sql = $sql."and `entrylist`.`title` like binary '%$this->keyword%'";
			}*/			
		}
		// キーワード検索の場合
		else
		{
			if($this->keyword != "" && $this->keyword != null)
			{
				$keyword_zen = mb_convert_kana($this->keyword,"K","utf-8");
				$keyword_han = mb_convert_kana($this->keyword,"k","utf-8");
			}
			else
			{
				$keyword_zen = $this->keyword;
				$keyword_han = $this->keyword;
			}
			$sql = "select `entrylist`.`title`,`videolist`.`cid`,`videolist`.`video_id`,`videolist`.`time`,`videolist`.`up_date`,`videolist`.`view_cnt_y`,`videolist`.`rating_avg` ";
			$sql = $sql."from `entrylist`,`videolist` ";
			$sql = $sql."where (`entrylist`.`title` like binary '%$keyword_zen%' or `entrylist`.`title` like binary '%$keyword_han%') ";
			$sql = $sql."and `videolist`.`site_id` = 1 ";
			$sql = $sql."and `videolist`.`open_flag` = 1 ";
			$sql = $sql."and `entrylist`.`open_flag` = 1 ";
			$sql = $sql."and `videolist`.`cid` is not null ";
			$sql = $sql."and `entrylist`.`entry_id` = `videolist`.`entry_id` ";
		}
		return $sql;
	}
		
	function getOrderSql()
	{
		$sql = "";
		
		// 新着
		if($this->orderType == "1")
		{
			$sql = " order by up_date desc ";
		}
		// 人気
		else if ($this->orderType == "2")
		{
			$sql = " order by view_cnt_y desc ";
		}
		
		return $sql;
	}	
	
	function getResultList($pageNumb)
	{
		$fromRow = ($pageNumb-1) * $this->RECORD_NUMBER_PER_PAGE;
		$sql = $this->getSelectSql();
		$sql = $sql.$this->getOrderSql();
		$sql = $sql."limit ".$fromRow.",".$this->RECORD_NUMBER_PER_PAGE;
		
		$totalSql = $this->getSelectSql();		
		
		$resultL = mysql_query($sql);
		$rowsL = mysql_num_rows($resultL);
		$resultT = mysql_query($totalSql);
		$rowsT = mysql_num_rows($resultT);
		
		if($rowsL > 0)
		{
			$fromRowNumber = $fromRow + 1;
			$toRowNumber  = $fromRowNumber + $rowsL -1;
			$this->fistLineNum = $fromRowNumber;
			$this->lastLineNum = $toRowNumber;
			$this->totalLineNum = $rowsT;
			$this->getPageLinkHtml($rowsT);
			
			for ($i=0;$i<$rowsL;$i++)
			{
				$this->resultList = $this->resultList.Common::getRandomAd();
				
				$data = mysql_fetch_array($resultL);
					
				$this->resultList = $this->resultList."<a href=result.php?categoryId=".$this->categoryId."&videoId=".$data["video_id"]."&keyword=".urldecode($this->keyword).">".substr($data["title"],0,48)."...";
				$this->resultList = $this->resultList."</a><br><font size=1>";
				$this->resultList = $this->resultList."<a href=result.php?categoryId=".$this->categoryId."&videoId=".$data["video_id"]."&keyword=".urldecode($this->keyword)."><img src=http://i1.ytimg.com/vi/".$data["cid"]."/default.jpg align=left></a>";
				$this->resultList = $this->resultList."時間：".$data["time"]."秒<br>";
				$this->resultList = $this->resultList."登録：".$data["up_date"]."<br>";
				$this->resultList = $this->resultList."閲覧：".$data["view_cnt_y"]."回<br>";
				$this->resultList = $this->resultList."評価：".round($data["rating_avg"],0)."/5点<br>";
				$this->resultList = $this->resultList."<br clear=all></font>";				
			}
		}
		// データがない場合
		else
		{
			
		}
	}

	function getPageLinkHtml($totalRowNum)
	{
		$j = 0;
		$pageNum = 0;
		$firstPage = $this->nowPageNum;
		for ($i=$this->nowPageNum-1;$i < $totalRowNum/$this->RECORD_NUMBER_PER_PAGE;$i++)
		{
			$pageNum = $i+1;
			$j = $j + 1;

			$this->pageLinkHtml = $this->pageLinkHtml."<a href=result_list.php?categoryId=".$this->categoryId.
														"&nowPage=".$pageNum."&orderType=".$this->orderType.
														"&keyword=".$this->keyword."&tagId=".$this->tagId.">".$pageNum."|";														
			if ($j == 10)
			{
				break;
			}
		}
		
		if ($firstPage > 1)
		{
			$prePage = $pageNum - 20 + 1;
			if ($prePage < 0)
			{
				$this->pageLinkHtml = "<a href=result_list.php?categoryId=".$this->categoryId.
														"&nowPage=1"."&orderType=".$this->orderType.
														"&keyword=".$this->keyword."&tagId=".$this->tagId.">"."先へ".$this->pageLinkHtml;
			}
			else
			{
				$this->pageLinkHtml = "<a href=result_list.php?categoryId=".$this->categoryId.
														"&nowPage=".$prePage."&orderType=".$this->orderType.
														"&keyword=".$this->keyword."&tagId=".$this->tagId.">"."先へ".$this->pageLinkHtml;				
			}
		}
		if ($pageNum < $totalRowNum/$this->RECORD_NUMBER_PER_PAGE)
		{
			$nextPage = $pageNum + 1;
			$this->pageLinkHtml = $this->pageLinkHtml."<a href=result_list.php?categoryId=".$this->categoryId.
														"&nowPage=".$nextPage."&orderType=".$this->orderType.
														"&keyword=".$this->keyword."&tagId=".$this->tagId.">"."次へ";
		}			
	}
	
	function insertOrUpdate_search_history($keyword)
	{
		$weekly_id = date("W");
		
		$search_date = date("Y/m/d");
		
		$sql = "";
			
		// 全角を半角を変換する
		$keyword_han = mb_convert_kana($keyword,"k","utf-8");
				
		// データがある場合
		if($this->hasThisKeyword($keyword_han) == true)
		{			
			$sql = "update `search_history` set `search_cnt` = search_cnt + 1,`last_searchtime` = now() where `search_word` = '".$keyword_han."' and `date` = '".$search_date."' and `weekly_id` = ".$weekly_id;
		}
		// データが無い場合
		else
		{
			$sql = "insert into `search_history` (`search_word`,`date`,`weekly_id`,`search_cnt`,`last_searchtime`) values ('".$keyword_han."','".$search_date."',".$weekly_id.",1,now())";
		}
		
		$result = mysql_query($sql);
	}
	
	function hasThisKeyword($keyword)
	{
		$weekly_id = date("W");
		
		$search_date = date("Y/m/d");
		
		$sql = "select * from `search_history` where `search_word` = '".$keyword."' and `date` = '".$search_date."' and `weekly_id` = ".$weekly_id;

		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		
		if($rows > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
?>